package com.fg.cloud.manager.service.product.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fg.cloud.common.construction.CategoryTree;
import com.fg.cloud.common.finals.Agency;
import com.fg.cloud.common.shiro.UserUtils;
import com.fg.cloud.manager.entry.BaseEntity;
import com.fg.cloud.manager.entry.agency.vo.AgencyUserVo;
import com.fg.cloud.manager.entry.product.ProductCategoryEntity;
import com.fg.cloud.manager.mapper.BaseMapper;
import com.fg.cloud.manager.mapper.agency.AgencyProductMapper;
import com.fg.cloud.manager.mapper.product.ProductCategoryMapper;
import com.fg.cloud.manager.service.BaseServiceImpl;
import com.fg.cloud.manager.service.product.ProductCategoryService;

/**
 * @author liushengchao
 * @Description: 商品分类接口实现 
 * @date 2017年6月20日上午11:49:56
 *
 */
@Service
public class ProductCategoryServiceImpl extends BaseServiceImpl implements ProductCategoryService{

	@Autowired
	private ProductCategoryMapper categoryMapper ;
	
	@Autowired
	private AgencyProductMapper category_priceMapper;
	
	@Override
	protected <T extends BaseEntity> BaseMapper getBaseMapper() {
		return categoryMapper;
	}

	/**
	 * @Description: 根据父节点ID查询子分类信息 
	 * @param parentId 父节点ID
	 * @return List<ProductCategoryEntity> 子分类信息列表
	 * @author liushengchao
	 * @date 2017年6月20日下午2:35:40
	 * @throws 
	 */
	@Override
	public List<ProductCategoryEntity> selectByParentId(Long parentId) {
		return categoryMapper.selectByParentId(parentId);
	}

	/**
	 * 级联删除商品分类
	 */
	@Override
	@Transactional
	public int deleteByCategoryNumber(Map<String, Object> condition) {
		//查询待删除的所有ID
		Long[] ids = categoryMapper.findIds(Long.valueOf(condition.get("id").toString()),
				condition.get("number").toString());
		Long flag = categoryMapper.findCountByCategoryNumber(condition);
		if(flag > 0){
			return -1;
		}
		//删除当前分类
		int count = categoryMapper.deleteByCategoryNumber(condition);
		//追加删除当前厂商所对应的所有该分类信息
		category_priceMapper.batchDeletesByCategorys(Agency.getTableName(
				Long.valueOf(condition.get("facId").toString())), 
				Long.valueOf(condition.get("facId").toString()), null, ids);
		return count;
	}

	/**
	 * 查询全部数据并组成树形结构
	 */
	@Override
	public CategoryTree selectByFacId(Long facId) {
		List<ProductCategoryEntity> list = categoryMapper.selectByFacId(facId);
		CategoryTree tree =null;
		if(list.size() >0){
			tree = toTree(list);
		}
		return tree;
	}

	/**
	 * 把list转换成tree
	 * */
	public CategoryTree toTree(List<ProductCategoryEntity> list){
		Map<Long,CategoryTree> treeMap = new HashMap<Long,CategoryTree>();
		Long endid = null; // 获取最外边节点
		
		for(int i = 0;i<list.size();i++){
			CategoryTree tree = new CategoryTree();
			tree.setId(list.get(i).getId());
			tree.setPid(list.get(i).getPid());
			tree.setName(list.get(i).getName());
			tree.setNumber(list.get(i).getNumber());
			tree.setFacId(list.get(i).getFacId());
			tree.setSeq(list.get(i).getSeq());
			tree.setIcoName(list.get(i).getIcoName());
			
			if(list.get(i).getPid()==0 || list.get(i).getPid()==null){// 获取根节点
				endid = list.get(i).getId();
			}
			treeMap.put(list.get(i).getId(),tree);
		}
		
		for(Long id : treeMap.keySet()){
			if(treeMap.get(id).getPid()==0 || treeMap.get(id).getId()==null){
				treeMap.put(id, treeMap.get(id));
			}else{
				long pid = treeMap.get(id).getPid();
				CategoryTree childTree =  treeMap.get(pid);
				List<CategoryTree> childList = childTree.getChildren() == null ? new ArrayList<CategoryTree>() : childTree.getChildren();
				childList.add(treeMap.get(id));
				Collections.sort(childList);
				childTree.setChildren(childList);
				treeMap.put(pid, childTree);
			}
		}
		
		CategoryTree tree = treeMap.get(endid);
		return tree;
	}

	/**
	 * 获取前三层并封装树形结构
	 */
	@Override
	public List<CategoryTree> selectThreeByFacId(Map<String, Object> usmap) {
		AgencyUserVo user = (AgencyUserVo) UserUtils.getCurrentUser();
		//user.setPlaceorder(1);
		List<ProductCategoryEntity> list;
		if(user.getId() == null){// 游客
			list = categoryMapper.selectThreeByFacIdFree(usmap);
		}else{
			if(user.getPlaceorder() == 1){
				list = categoryMapper.selectThreeByFacId(usmap);
			}else{
				list = categoryMapper.selectThreeByFacIdFree(usmap);
			}
		}
		if(list.size() >0){
			CategoryTree tree = toTree(list);
			return tree.getChildren();
		}else{
			return new ArrayList<CategoryTree>();
		}
	}

	/**
	 * 删除
	 * */
	@Override
	@Transactional
	public int delete(Map<String, Object> condition) {
		return categoryMapper.delete(condition);
	}

	@Override
	public int findnum(ProductCategoryEntity categoryEntity) {
		return categoryMapper.findnum(categoryEntity);
	}

	@Override
	public List<CategoryTree> findAllCategory(Map<String, Object> usmap) {
		AgencyUserVo user = (AgencyUserVo) UserUtils.getCurrentUser();
		List<ProductCategoryEntity> list;
		if(user.getId() == null ){// 游客
			list = categoryMapper.findCategoryFree(usmap);
		}else{// 经销商
			if(user.getPlaceorder() == 1){
				list = categoryMapper.findAllCategory(usmap);
			}else{
				list = categoryMapper.findCategoryFree(usmap);
			}
		}
		 
		if(list.size() >0){
			List<CategoryTree> list1 = new ArrayList<CategoryTree>();
			CategoryTree tree = toTree(list);
			list1.add(tree);
			return list1;
		}else{
			return new ArrayList<CategoryTree>();
		}
	}

	@Override
	@Transactional
	public int updates(ProductCategoryEntity categoryEntity) {
		String oldNumber = categoryMapper.findCategoryNumber(categoryEntity);
		String number = oldNumber+"_"+categoryEntity.getId();// 用于判断
		int a = categoryMapper.update(categoryEntity);// 修改当前分类
		int b = 1;
		if(categoryEntity.getNumber() != null && !oldNumber.equals(categoryEntity.getNumber())) {
			b = categoryMapper.updates(oldNumber, categoryEntity.getNumber(),number);//修改子分类的编码
		}
		if(a>0 && b>0){
			return 1;
		}
		return 0;
	}
	
	/**
	 * 通过分类名称查询分类信息
	 */
	@Override
	public ProductCategoryEntity selectCategoryByName(String categoryName, Long facId) {
		return categoryMapper.selectCategoryByName(categoryName, facId);
	}
}
